//
//  BattleshipGameBoardControl.java
//  NetworkBattleship
//
//  Created by Alan Joyce on 12/17/06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//
package gamefiles;


public class BattleshipGameBoardControl extends Object
    {
//Declare variables.
	public final int SHIP_AIRCRAFT_CARRIER = 0;
	public final int SHIP_BATTLESHIP = 1;
	public final int SHIP_CRUISER = 2;
	public final int SHIP_PATROL_BOAT = 3 ;
	public final int SHIP_SUBMARINE = 4;
	
	public final int SHIP_SIZE_AIRCRAFT_CARRIER = 5;
	public final int SHIP_SIZE_BATTLESHIP = 4;
	public final int SHIP_SIZE_CRUISER = 3;
	public final int SHIP_SIZE_PATROL_BOAT = 2;
	public final int SHIP_SIZE_SUBMARINE = 3;
	
	public final int hitsForVictory = SHIP_SIZE_AIRCRAFT_CARRIER + SHIP_SIZE_BATTLESHIP + SHIP_SIZE_CRUISER + SHIP_SIZE_PATROL_BOAT + SHIP_SIZE_SUBMARINE;
	
	public final int ORIENT_UP = 0;
	public final int ORIENT_RIGHT = 1 ;
	public final int ORIENT_DOWN = 2;
	public final int ORIENT_LEFT = 3;
	
	
	private BattleshipGameBoard gameBoard;
	
	
	public BattleshipGameBoardControl(int gameBoardSize) {
		gameBoard = new BattleshipGameBoard(gameBoardSize);
		gameBoard.createCells();
	}
	
	public int getCellState(int player, int xPos, int yPos) {
		return gameBoard.getCellState(player, xPos, yPos);
	}
	
	public int setCellState(int player, int xPos, int yPos, int state) {
		return gameBoard.setCellState(player, xPos, yPos, state);
	}
	
	public int placeShip(int player, int shipType, int topLeftXPos, int topLeftYPos, int orient) {
		if (orient == ORIENT_DOWN) {
			return gameBoard.shipCellsVertical(player, topLeftYPos, topLeftYPos + getShipSize(shipType) - 1 , topLeftXPos);
		}
		else if(orient == ORIENT_UP) {
			return gameBoard.shipCellsVertical(player, topLeftYPos, topLeftYPos - getShipSize(shipType) + 1 , topLeftXPos);
		}
		else if(orient == ORIENT_RIGHT) {
			return gameBoard.shipCellsHorizontal(player, topLeftXPos, topLeftXPos + getShipSize(shipType) - 1 , topLeftYPos);
		}
		else if(orient == ORIENT_LEFT) {
			return gameBoard.shipCellsHorizontal(player, topLeftXPos, topLeftXPos - getShipSize(shipType) + 1 , topLeftYPos);
		}
		
		return -1;
	}
	
	//Returns -1 if error, 0 if no ship hit, 1 if ship hit.
	public int fire(int player, int xPos, int yPos) {
		return gameBoard.fireCell (player, xPos, yPos);
	}
	
	public int getShipSize(int shipType)
	{
		switch(shipType) {
			case SHIP_AIRCRAFT_CARRIER:
				return SHIP_SIZE_AIRCRAFT_CARRIER ;
			case SHIP_BATTLESHIP:
				return SHIP_SIZE_BATTLESHIP;
			case SHIP_CRUISER:
				return SHIP_SIZE_CRUISER;
			case SHIP_PATROL_BOAT:
				return SHIP_SIZE_PATROL_BOAT;
			case SHIP_SUBMARINE:
				return SHIP_SIZE_SUBMARINE;
			default:
				return 0;
		}
	}
	
//Returns true if victory, false if no victory.
	public boolean checkVictory(int player) {
		return gameBoard.checkVictoryCells(player, hitsForVictory);
	}
} 